﻿#region Header

// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version>$Revision: 1965 $</version>
// </file>

#endregion Header

namespace ICSharpCode.TextEditor
{
    using System.Collections;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    /// <summary>
    /// Contains brushes/pens for the text editor to speed up drawing. Re-Creation of brushes and pens
    /// seems too costly.
    /// </summary>
    public class BrushRegistry
    {
        #region Fields

        static Hashtable brushes = new Hashtable();
        static Hashtable dotPens = new Hashtable();
        static Hashtable pens = new Hashtable();

        #endregion Fields

        #region Methods

        public static Brush GetBrush(Color color)
        {
            if (!brushes.Contains(color)) {
                Brush newBrush = new SolidBrush(color);
                brushes.Add(color, newBrush);
                return newBrush;
            }
            return brushes[color] as Brush;
        }

        public static Pen GetDotPen(Color bgColor, Color fgColor)
        {
            bool containsBgColor = dotPens.Contains(bgColor);
            if (!containsBgColor || !((Hashtable)dotPens[bgColor]).Contains(fgColor)) {
                if (!containsBgColor) {
                    dotPens[bgColor] = new Hashtable();
                }

                HatchBrush hb = new HatchBrush(HatchStyle.Percent50, bgColor, fgColor);
                Pen newPen = new Pen(hb);
                ((Hashtable)dotPens[bgColor])[fgColor] = newPen;
                return newPen;
            }
            return ((Hashtable)dotPens[bgColor])[fgColor] as Pen;
        }

        public static Pen GetPen(Color color)
        {
            if (!pens.Contains(color)) {
                Pen newPen = new Pen(color);
                pens.Add(color, newPen);
                return newPen;
            }
            return pens[color] as Pen;
        }

        #endregion Methods
    }
}